import pytest



@pytest.fixture
def file_read():
    print("打开文件")
    fo=open("test.txt","r",encoding="utf-8")
    # 返回一个值
    yield fo

    # 测试方法结束后，执行yield后面的内容
    print("后置操作")
    fo.close()

@pytest.fixture
def file_write():
    print("打开文件")
    fo=open("test.txt","w",encoding="utf-8")

    yield fo
    print("关闭文件")
    fo.close()


# 注意 fixture后置操作是当前测试方法执行完之后，统一执行
def test(file_read,file_write):
    w=file_write
    w.write("nihao")
    w.close()

    r=file_read
    str=r.read()
    print(str)


